import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys: [],
		
		// 搜索表单列表
		forms : [
			{ label: '公司',		name: 'code' },
		],
		// 提交表单列表
		formData : [
			{ label:'科室',		name: 'team_id',	half:true,	type: 'select', idStr:'uuid', required:true,
				onChange: ({value}) => {
					// 获取岗位
					value && this.getSelectd({api: 'project-team/jobselect', param: {uuid: value}, name: 'project_id', type: '岗位', clear: ['project_id']})
				}
			},
			{ label:'岗位',		name: 'project_id', 		half:true,	type:'select', idStr: 'uuid',  p: '请先选择科室', required:true },
			{ label: '分公司',	name: 'to_comp_id', 	 	half:true,	type: 'select',	required:true,
				onChange: ({value}) => {
					// 获取分公司科室
					value && this.getSelectd({api: 'lis-outsourcing-map/team', param: {comp_id: value}, name: 'to_team_id', type: '科室', clear: ['to_team_id', 'to_project_id']})
				}
			},
			{ label: '分公司科室',		name: 'to_team_id', 			half:true,	type: 'select',	p: '请先选择分公司',	required:true,
				onChange: ({value}) => {
					// 获取分公司岗位
					const { to_comp_id: comp_id } = this.formRef.getValue(['to_comp_id'])
					value && this.getSelectd({api: 'lis-outsourcing-map/project', param: {comp_id, team_id: value}, name: 'to_project_id', type: '岗位'})
				}
			},
			{ label: '分公司岗位',		name: 'to_project_id', 			half:true,	type: 'select',	p: '请先选择科室',	required:true },
			{ label: '是否回传报告单',	name: 'postback_report', 		half:true,	type: 'radio',	data: [{name: '是', value: '1'}, {name: '否', value: '0'}],	required:true },
			{ label: '是否自己出报告',	name: 'report_self', 			half:true,	type: 'radio',	data: [{name: '是', value: '1'}, {name: '否', value: '0'}],	required:true },
			{ label: '检测人',			name: 'ts_user_id', 			half:true,	type: 'select' },
			{ label: '审核人',			name: 'ts_audit_id', 			half:true,	type: 'select' },
			{ label: '批准人',			name: 'ts_approve_id', 			half:true,	type: 'select' },
			// { label: '备注',		name: 'remark', 	full:true,		type:'textarea' },
		],
	}
	
	model = {}
	
	componentDidMount(){
		this.fetch()
		this.getSelect()
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'lis-outsourcing-map/index')
	
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		$cache.then( async f => {
			// 公司
			$fn.setRow(formData, 'to_comp_id',  await f.getCache({ api:'lis-outsourcing-map/company' }) )
			// 科室
			$fn.setRow(formData, 'team_id', await f.getCache({ api:'ke_shi' } ) )
			// 签名人
			const signature = await f.getCache( { api:'employee' })
			$fn.setRow(formData, 'ts_user_id', signature )
			$fn.setRow(formData, 'ts_audit_id', signature )
			$fn.setRow(formData, 'ts_approve_id', signature )
			
			this.setState({ formData })
		})
	}
	
	// 获取弹窗下拉数据
	getModalSelect = async (rows) => {
		const { team_id, to_comp_id, to_team_id } = rows
		// 获取岗位
		this.getSelectd({api: 'project-team/jobselect', param: {uuid: team_id}, name: 'project_id', type: '岗位' })
		// 获取分公司科室
		this.getSelectd({api: 'lis-outsourcing-map/team', param: {comp_id: to_comp_id}, name: 'to_team_id', type: '科室' })
		// 获取分公司岗位
		this.getSelectd({api: 'lis-outsourcing-map/project', param: {comp_id: to_comp_id, team_id: to_team_id}, name: 'to_project_id', type: '岗位'})
	}
	
	// 获取 岗位 或者 科室
	getSelectd = (data) => {
		const { api, param, name, type, clear } = data
		window.$http.submit(null, api, { param, isPull:true }).then(data=>{
			const { formData } = this.state
			if(Array.isArray(data)){
				$cache.then( async f => {
					// 清理已选内容
					clear && this.formRef.reset(clear)
					// 所属类型
					$fn.setRow(formData, name, data)
					$fn.getRow(formData, name).p = `请选择所属${type}`
					//
					this.setState({ formData })
				})
			}
		})
	}
	
	// 表单
	cols = [
		{ title: '公司',		dataIndex: 'comp_name', 		width:50 },
		{ title: '科室',		dataIndex: 'team_name', 		width:50 },
		{ title: '岗位', 		dataIndex: 'project_name', 		width:150 },
		{ title: '分公司', 		dataIndex: 'to_comp_name', 		width:150 },
		{ title: '分公司科室', 	dataIndex: 'to_team_name', 		width:150 },
		{ title: '分公司岗位', 	dataIndex: 'to_project_name', 	width:150 },
		TableControls.controls.call(this, { width:100, align:'left'})
	]
	
	// 删除
	onDel = uuid => Delete.call(this, 'lis-outsourcing-map/del', { uuid, isKeys: !uuid, disabled: this.props.disabled })
	
	// 表格操作按钮
	table_controls = (text,{ uuid, to_comp_id, project_id, to_project_id }) => {
		return [
			TableControls.edit.call(this, 'lis-outsourcing-map/info', { uuid }, {
				load: (rows) => {
					this.getModalSelect(rows)
				}
			}),				// 编辑
			TableControls.pushTo.call(this, '项目管理',  `project-management?vendor_comp_id=${to_comp_id}&vendor_project_id=${to_project_id}&project_id=${project_id}` ),
		]
	}
	// 操作按钮
	controls = () => {
		const { selectKeys } = this.state
		return [
			Controls.add.call(this, { click: () => this.formRef.setValue({ postback_report: '1' }) }),			// 添加
			this.onDel(selectKeys)						// 删除
		]
	}
	
	render(){
		return (
			<>
				<Page title='集团内部外包设置' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'lis-outsourcing-map/add', editApi:'lis-outsourcing-map/edit', labelWidth:'s8', width: 700 }) }
			</>
		)
	}
}

export default Index